package qsc.algorithm.floyd;

import java.util.Arrays;

/**
 * @auther QiuShangcheng
 * @create 2021/5/5
 */
public class Review {

    public static void main(String[] args) {
        // 测试看看图是否创建成功
        char[] vertex = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
        // 创建邻接矩阵
        int[][] matrix = new int[vertex.length][vertex.length];
        final int N = 65535;
        matrix[0] = new int[] { 0, 5, 7, N, N, N, 2 };
        matrix[1] = new int[] { 5, 0, N, 9, N, N, 3 };
        matrix[2] = new int[] { 7, N, 0, N, 8, N, N };
        matrix[3] = new int[] { N, 9, N, 0, N, 4, N };
        matrix[4] = new int[] { N, N, 8, N, 0, 5, 4 };
        matrix[5] = new int[] { N, N, N, 4, 5, 0, 6 };
        matrix[6] = new int[] { 2, 3, N, N, 4, 6, 0 };
        Graph graph = new Graph(vertex, matrix, vertex.length - 1);
        floyd(graph);
    }
    public static void floyd(Graph graph){
//        int[][] prev = new int[graph.getVertex().length][graph.getVertex().length];
//        for (int i = 0; i < graph.getVertex().length; i++) {
//            Arrays.fill(prev[i],i);
//        }

        for (int k = 0; k < graph.getVertex().length; k++) {
            for (int j = 0; j < graph.getVertex().length; j++) {
                for (int i = 0; i < graph.getVertex().length; i++) {
                    int temp = graph.getMatrix()[i][k] + graph.getMatrix()[k][j];
                    if (graph.getMatrix()[i][j] > temp){
                        graph.getMatrix()[i][j] = temp;
//                        prev[i][j] = prev[k][j];
                    }
                }
            }
        }
        System.out.println("floyd->最终各个村庄最短距离:");
        for (int[] temp : graph.getMatrix()
        ) {
            System.out.println(Arrays.toString(temp));
        }
//        System.out.println("前置节点矩阵为：");
//        for (int[] temp : prev
//        ) {
//            System.out.println(Arrays.toString(temp));
//        }
    }
}
